<?php

namespace App\Http\Controllers\Backend;

use App\Admin;
use App\Article;
use App\Base;
use App\Game;
use Illuminate\Http\Request;

use App\Http\Requests;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Redirect;

class IndexController extends BasicController
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        return view('backend.index.index');
    }


    public function monitor(){
        return view('backend.monitor.index');
    }

    public function homemanage(){
        $Home=Base::where('name','=','home')->first();
        return view('backend.index.homemanage')->with('home',$Home);
    }

    public function posthomemanage(){
        $param=\Input::get('home');
        $Home=Base::where('name','=','home')->first();
        $Home->value=$param;
        $rs=$Home->save();
        $data=array();
        $data['type']='s';
        $data['message']='修改成功';
        return redirect()->back()->with('message',$data);
    }

    public function gamemanage(){
        $games=Game::paginate(10);
        return view('backend.index.gamemanage')->with('games',$games);
    }

    public function delgamemanage(){
        $id=\Input::get('id');
        Game::destroy($id);
        $data=array();
        $data['type']='s';
        $data['message']='删除成功';
        return redirect()->back()->with('message',$data);
    }

    public function addgamemanage(){
        return view('backend.index.addgamemanage');
    }

    public function doaddgamemanage(Request $request){
        $title=\Input::get('title');
        $type=\Input::get('type');
        $img = $request->file('img');
        $img1 = $request->file('img1');

        $rs='';
        if(isset($img)){
            if ($img->isValid()) {
                $clientName = $img->getClientOriginalName();
                $entension = $img -> getClientOriginalExtension();
                $newName = md5(date('ymdhis').$clientName).".".$entension;
                $rs=$img -> move(base_path().'/public/uploads/',$newName);
            }

        }
        $rs1='';
        if(isset($img1)){
            if ($img1->isValid()) {
                $clientName1 = $img1->getClientOriginalName();
                $entension1 = $img1 -> getClientOriginalExtension();
                $newName1 = md5(date('ymdhis').$clientName1).".".$entension1;
                $rs1=$img1-> move(base_path().'/public/uploads/',$newName1);
            }
        }

        $sort=\Input::get('sort');
        $num=\Input::get('num');
        $Game=new Game();
        $Game->title=$title;
        $Game->type=$type;
        if($rs){
            $Game->img=$newName;
        }
        if($rs1){
            $Game->img1=$newName1;
        }
        $Game->sort=$sort;
        $Game->num=$num;
        $Game->save();
        $data=array();
        $data['type']='s';
        $data['message']='添加成功';
        return redirect()->back()->with('message',$data);
    }

    public function editgamemanage(){
        $id=\Input::get('id');
        $game=Game::find($id);
        return view('backend.index.editgamemanage')->with('game',$game);
    }

    public function doeditgamemanage(Request $request){
        $title=\Input::get('title');
        $type=\Input::get('type');
        $img = $request->file('img');
        $img1 = $request->file('img1');

        $id=\Input::get('id');
        $Game=Game::find($id);

        $rs='';
        if(isset($img)){
            if ($img->isValid()) {
                $clientName = $img->getClientOriginalName();
                $entension = $img -> getClientOriginalExtension();
                $newName = md5(date('ymdhis').$clientName).".".$entension;
                $rs=$img -> move(base_path().'/public/uploads/',$newName);
            }

        }
        $rs1='';
        if(isset($img1)){
            if ($img1->isValid()) {
                $clientName1 = $img1->getClientOriginalName();
                $entension1 = $img1 -> getClientOriginalExtension();
                $newName1 = md5(date('ymdhis').$clientName1).".".$entension1;
                $rs1=$img1-> move(base_path().'/public/uploads/',$newName1);
            }
        }

        $sort=\Input::get('sort');
        $num=\Input::get('num');
        $Game->title=$title;
        $Game->type=$type;
        if($rs){
            $Game->img=$newName;
        }
        if($rs1){
            $Game->img1=$newName1;
        }
        $Game->sort=$sort;
        $Game->num=$num;
        $Game->save();
        $data=array();
        $data['type']='s';
        $data['message']='修改成功！';
        return redirect()->back()->with('message',$data);
    }


    public function activity(){
        $articles=Article::orderBy('created_at', 'desc')->paginate(10);
        return view('backend.index.activity')->with('articles',$articles);
    }

    public function addactivity(){
        return view('backend.index.addactivity');
    }

    public function postaddactivity(Request $request){
        $title=\Input::get('title');
        $cid=\Input::get('cid');

        $pic = $request->file('pic');
        $rs='';
        if(isset($pic)){
            if ($pic->isValid()) {
                $clientName = $pic->getClientOriginalName();
                $entension = $pic -> getClientOriginalExtension();
                $newName = md5(date('ymdhis').$clientName).".".$entension;
                $rs=$pic -> move(base_path().'/public/uploads/ac/',$newName);
            }
        }

        $source=\Input::get('source');
        $content=\Input::get('content');
        $sort=\Input::get('sort');
        $article=new Article;
        $article->title=$title;
        $article->cid=$cid;
        if($rs){
            $article->pic=$newName;
        }
        $article->source=$source;
        $article->content=$content;
        $article->sort=$sort;
        $article->save();

        $data=array();
        $data['type']='s';
        $data['message']='添加成功！';
        return redirect()->back()->with('message',$data);
    }

    public function editactivity(){
        $id=\Input::get('id');
        $article=Article::find($id);
        return view('backend.index.editactivity')->with('article',$article);
    }

    public function posteditactivity(Request $request){
        $id=\Input::get('id');
        $article=Article::find($id);
        $title=\Input::get('title');
        $cid=\Input::get('cid');

        $pic = $request->file('pic');
        $rs='';
        if(isset($pic)){
            if ($pic->isValid()) {
                $clientName = $pic->getClientOriginalName();
                $entension = $pic -> getClientOriginalExtension();
                $newName = md5(date('ymdhis').$clientName).".".$entension;
                $rs=$pic -> move(base_path().'/public/uploads/ac/',$newName);
            }
        }

        $source=\Input::get('source');
        $content=\Input::get('content');
        $sort=\Input::get('sort');
        $article->title=$title;
        $article->cid=$cid;
        if($rs){
            $article->pic=$newName;
        }
        $article->source=$source;
        $article->content=$content;
        $article->sort=$sort;
        $article->save();

        $data=array();
        $data['type']='s';
        $data['message']='修改成功！';
        return redirect()->back()->with('message',$data);
    }

    public function delactivity(){
        $id=\Input::get('id');
        Article::destroy($id);
        $data=array();
        $data['type']='s';
        $data['message']='删除成功';
        return redirect()->back()->with('message',$data);
    }


    public function repassword(){
        return view('backend.index.repassword');
    }

    public function postrepassword(){
        $password=\Input::get('password');
        $password1=\Input::get('password1');
        $password2=\Input::get('password2');
        $admin=Admin::find(1);
        if(!\Hash::check($password,$admin->password)){
            $data['type']='w';
            $data['message']='原密码错误！';
            return redirect()->back()->with('message',$data);
        }
        if($password1!=''&&$password1==$password2)
        {
            $neWpassword=\Hash::make($password1);
            $admin->password=$neWpassword;
            $admin->save();
            $data['type']='s';
            $data['message']='修改密码成功！';
            return redirect()->back()->with('message',$data);
        }
        $data['type']='w';
        $data['message']='failed！';
        return redirect()->back()->with('message',$data);
    }


    public function paytest(){
        echo 'paytest';
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        //
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        //
    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        //
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        //
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
        //
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        //
    }
}
